package com.atzc.sink;

import com.atzc.bean.CarListLogBean;

import com.atzc.utils.mysql.MysqlConnect;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author yhm
 * @create 2021-04-11 20:57
 */
public class CarListLogSinkMysql extends RichSinkFunction<CarListLogBean> {

    private PreparedStatement ps = null;
    private Connection connection = MysqlConnect.getConnection();

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
        try {
            String sql = "insert into carlist_log_new( create_time,requestid,mac,os,imei,oaid,`schema`,publiccitycode,appname,devicename,publictoken,androidid,citycode,scenecode,searchtype,pagenum,pagesize,isfirst,filtercondition,publiclatitude,publiclongitude,appversion,osversion,appchannelid,create_date,import_date,update_time,memno,meminfocache,searchcontent,h5platform,) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            ps = connection.prepareStatement(sql);
        }catch (SQLException e){
            System.out.println("error:车辆列表日志编译sql出错了");
        }

    }

    @Override
    public void invoke(CarListLogBean carListLogBean, Context context) throws Exception {
        //设置字段
        try {
            ps.setString(1,carListLogBean.getCreate_time());
            ps.setString(2,carListLogBean.getRequestid());
            ps.setString(3,carListLogBean.getMac());
            ps.setString(4,carListLogBean.getOs());
            ps.setString(5,carListLogBean.getImei());
            ps.setString(6,carListLogBean.getOaid());
            ps.setString(7,carListLogBean.getSchema());
            ps.setString(8,carListLogBean.getPubliccitycode());
            ps.setString(9,carListLogBean.getAppname());
            ps.setString(10,carListLogBean.getDevicename());
            ps.setString(11,carListLogBean.getPublictoken());
            ps.setString(12,carListLogBean.getAndroidid());
            ps.setString(13,carListLogBean.getCitycode());
            ps.setString(14,carListLogBean.getScenecode());
            ps.setString(15,carListLogBean.getSearchtype());
            ps.setString(16,carListLogBean.getPagenum());
            ps.setString(17,carListLogBean.getPagesize());
            ps.setString(18,carListLogBean.getIsfirst());
            ps.setString(19,carListLogBean.getFiltercondition());
            ps.setString(20,carListLogBean.getPubliclatitude());
            ps.setString(21,carListLogBean.getPubliclongitude());
            ps.setString(22,carListLogBean.getAppversion());
            ps.setString(23,carListLogBean.getOsversion());
            ps.setString(24,carListLogBean.getAppchannelid());
            ps.setString(25,carListLogBean.getCreate_date());
            ps.setString(26,carListLogBean.getImport_date());
            ps.setString(27,carListLogBean.getUpdate_time());
            ps.setString(28,carListLogBean.getMemno());
            ps.setString(29,carListLogBean.getMeminfocache());
            ps.setString(30,carListLogBean.getSearchcontent());
            ps.setString(31,carListLogBean.getH5platform());
        }catch (Exception e){
            System.out.println("error:车辆列表插入数据出错了");
        }

        // 执行写入
        ps.execute();
    }
}